package br.com.sistema.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import br.com.sistema.conexao.Entity;
import br.com.sistema.models.Pais;

public class PaisDao implements IDao<Pais> {

	@Override
	public Pais getById(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Pais> getByCriteria(Pais o) throws Exception {
		
		try {
			
			EntityManager em = Entity.createEm();
			List<Pais> list;
			
			try {
				
				CriteriaBuilder cb = em.getCriteriaBuilder();
				CriteriaQuery c = cb.createQuery(Pais.class);
				Root<Pais> a = c.from(Pais.class);
				c.select(a);

				List<Predicate> predicates = new ArrayList<Predicate>();
				
				if(o.getPaisnome() != null){
					predicates.add(cb.like(a.<String>get("paisnome"), "%"+o.getPaisnome()+"%"));
				}
				
				if(o.getPaiscodnum() != null){
					predicates.add(cb.like(a.<String>get("paiscodnum"), "%"+o.getPaiscodnum()+"%"));
				}
				
				if(o.getPaisalf2() != null){
					predicates.add(cb.like(a.<String>get("paisalf2"), "%"+o.getPaisalf2()+"%"));
				}
				
				if(o.getPaisalf3() != null){
					predicates.add(cb.like(a.<String>get("paisalf3"), "%"+o.getPaisalf3()+"%"));
				}
				
				c.where(cb.and(predicates.toArray(new Predicate[]{})));
				
				TypedQuery<Pais> query = em.createQuery(c);
				list = query.getResultList();
				
				return list;

			} catch (Exception e) {
				throw e;
			}finally{
				em.close();
			}
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void remove(List<Pais> list) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Pais> getAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pais manter(Pais o) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pais insere(Pais o) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pais altera(Pais o) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	
	
}
